package com.zhanbo.zhanbowymusic.main.mine.recently;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mwx.baselib.base.BaseFragment;
import com.mwx.baselib.bean.SongBean;
import com.mwx.baselib.constant.Constant;
import com.mwx.baselib.utils.MLog;
import com.mwx.baselib.utils.SDFileUtility;
import com.zhanbo.zhanbowymusic.MyApplication;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentRecentlyPlayedBinding;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 最近播放fragment
 */
public class RecentlyPlayedFragment extends BaseFragment<FragmentRecentlyPlayedBinding> {

//    private String[] mTitlesArrays = {"单曲", "播客", "歌单", "专辑"};
    private String[] mTitlesArrays = {"单曲"};

    private ArrayList<Fragment> fragments;
    private SingleSongRecentlyFragment singleSongRecentlyFragment;

    @Override
    protected void init(Bundle savedInstanceState) {
        initData();
        initView();
        initListener();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);

    }

    @Override
    protected Object getLayout() {
        return R.layout.fragment_recently_played;
    }

    @Override
    protected void initData() {
        SDFileUtility sdHelper2 = new SDFileUtility(MyApplication.getAppContext());
        try {
            String detail = sdHelper2.readFromSD(Constant.RECENTLY_PLAYED);
            if (!TextUtils.isEmpty(detail)) {
                List<SongBean> recentlyPlayedList = new Gson().fromJson(detail, new TypeToken<List<SongBean>>() {
                }.getType());
                binding.numSingleSong.setText(recentlyPlayedList.size()+"");
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    protected void initView() {
         singleSongRecentlyFragment =  SingleSongRecentlyFragment.newInstance();
        fragments = new ArrayList<>();
        fragments.add(singleSongRecentlyFragment);
//        fragments.add(PodcastsRecentlyFragment.newInstance());
//        fragments.add(SongListRecentlyFragment.newInstance());
//        fragments.add(AlbumRecentlyFragment.newInstance());
        binding.tabRecently.setViewPager(binding.vpRecentlyPlayed, mTitlesArrays, getActivity(), fragments);
    }

    @Override
    protected void initListener() {
        binding.tabRecently.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switchNum(position);
                switchButton(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        binding.btnRecentlyPlayAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                singleSongRecentlyFragment.playAll();
            }
        });
    }

    /**
     * 模块按钮隐藏
     * @param position -
     */
    public void switchButton(int position){
        binding.btnRecentlySingle.setVisibility(View.GONE);
        binding.btnRecentlyCleanAll.setVisibility(View.GONE);
        binding.btnRecentlyPlayAll.setVisibility(View.GONE);
        if (position == 0) {
            binding.btnRecentlySingle.setVisibility(View.VISIBLE);
        } else if (position == 1) {
            binding.btnRecentlyPlayAll.setVisibility(View.VISIBLE);
        } else if (position == 2) {
            binding.btnRecentlyCleanAll.setVisibility(View.VISIBLE);
        } else if (position == 3) {
            binding.btnRecentlyCleanAll.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 模块角标颜色更改
     *
     * @param position -模块坐标
     */
    public void switchNum(int position) {
        binding.numSingleSong.setTextColor(getResources().getColor(R.color.color_text));
        binding.numPodcasts.setTextColor(getResources().getColor(R.color.color_text));
        binding.numSongList.setTextColor(getResources().getColor(R.color.color_text));
        binding.numAlbum.setTextColor(getResources().getColor(R.color.color_text));
        if (position == 0) {
            binding.numSingleSong.setTextColor(getResources().getColor(R.color.color_283248));
        } else if (position == 1) {
            binding.numPodcasts.setTextColor(getResources().getColor(R.color.color_283248));
        } else if (position == 2) {
            binding.numSongList.setTextColor(getResources().getColor(R.color.color_283248));
        } else if (position == 3) {
            binding.numAlbum.setTextColor(getResources().getColor(R.color.color_283248));
        }
    }

    public static RecentlyPlayedFragment newInstance() {

        Bundle args = new Bundle();

        RecentlyPlayedFragment fragment = new RecentlyPlayedFragment();
        fragment.setArguments(args);
        return fragment;
    }
}